package com.distribute.framework.model;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.Field;
import java.util.Date; 
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import javax.annotation.Resource;

import com.distribute.framework.core.web.util.Page;
import com.distribute.framework.core.lang.*; 
import com.distribute.framework.core.web.helper.WebHelper.LzMap;
import com.distribute.framework.core.util.Function;
import com.distribute.framework.core.util.IbatisMap;
import com.distribute.framework.config.*;
import com.distribute.framework.plugin.*;

@namespace("api/oauth")
public @caption("用户授权") @database(DatabaseNames.distribute_sys) @source("distribute_sys") @table("srv_oauth") class SrvOauth implements java.io.Serializable
{
	private static final long serialVersionUID = 1l;
	private @caption("编号") @column("so_id")    @primary  @notnull  @note("  ") Long soId;// 主键 非空     
	private @caption("Token") @column("so_token")     @notnull  @note("  ") String soToken;// 非空     
	private @caption("帐号ID") @column("so_user_id")     @notnull  @note("  ") Long soUserId;// 非空     
	private @caption("API客户端") @column("so_api_client")     @notnull  @relate("$soApiClient") @RelateClass(SrvApiClient.class)  @note("  ") Long soApiClient;// 非空     
 	private transient SrvApiClient $soApiClient;//关联对象[API客户端]
	private @caption("三方标识") @column("so_ticket")      @note("  ") String soTicket;//     
	private @caption("三方参数") @column("so_params")      @note("  ") String soParams;//     
	private @caption("登录时间") @column("so_update_time")     @notnull  @note("  ") Date soUpdateTime;// 非空     
	 
	//默认构造函数
	public SrvOauth(){ 
	
	}
	
	//主键构造函数
	public SrvOauth(Long id){
		this.soId = id;
	}
	
	/**所有字段构造函数 
	 SrvOauth(
	 	Utils.getString("soToken")//Token [非空]
	 	,Utils.getLong("soUserId")//帐号ID [非空]
	 	,Utils.getLong("soApiClient")//API客户端 [非空]
	 	,Utils.getString("soTicket")//三方标识
	 	,Utils.getString("soParams")//三方参数
	 	,Utils.getDate("soUpdateTime")//登录时间 [非空]
	 )
	**/
	public SrvOauth(String soToken,Long soUserId,Long soApiClient,String soTicket,String soParams,Date soUpdateTime){
		this.soToken=soToken;
		this.soUserId=soUserId;
		this.soApiClient=soApiClient;
		this.soTicket=soTicket;
		this.soParams=soParams;
		this.soUpdateTime=soUpdateTime;
	}
	
	//设置非空字段
	public SrvOauth setNotNull(String soToken,Long soUserId,Long soApiClient,Date soUpdateTime){
		this.soToken=soToken;
		this.soUserId=soUserId;
		this.soApiClient=soApiClient;
		this.soUpdateTime=soUpdateTime;
		return this;
	}
	/** 编号 [非空]       **/
	public SrvOauth soId(Long soId){
		this.soId = soId;
		this.setSeted(F.soId);
		return this;
	}
	/** Token [非空]       **/
	public SrvOauth soToken(String soToken){
		this.soToken = soToken;
		this.setSeted(F.soToken);
		return this;
	}
	/** 帐号ID [非空]       **/
	public SrvOauth soUserId(Long soUserId){
		this.soUserId = soUserId;
		this.setSeted(F.soUserId);
		return this;
	}
	/** API客户端 [非空] [SrvApiClient]      **/
	public SrvOauth soApiClient(Long soApiClient){
		this.soApiClient = soApiClient;
		this.setSeted(F.soApiClient);
		return this;
	}
	/** 三方标识        **/
	public SrvOauth soTicket(String soTicket){
		this.soTicket = soTicket;
		this.setSeted(F.soTicket);
		return this;
	}
	/** 三方参数        **/
	public SrvOauth soParams(String soParams){
		this.soParams = soParams;
		this.setSeted(F.soParams);
		return this;
	}
	/** 登录时间 [非空]       **/
	public SrvOauth soUpdateTime(Date soUpdateTime){
		this.soUpdateTime = soUpdateTime;
		this.setSeted(F.soUpdateTime);
		return this;
	}
	
	
	/**
	* 克隆对象
	**/
	public SrvOauth clone(){
		SrvOauth clone = new SrvOauth();
		clone.soToken=this.soToken;
		clone.soUserId=this.soUserId;
		clone.soApiClient=this.soApiClient;
		clone.soTicket=this.soTicket;
		clone.soParams=this.soParams;
		clone.soUpdateTime=this.soUpdateTime;
		return clone;
	}
	
	
	/**
	 * 根据ID取用户授权
	 * @param id
	 * @return
	 */
	public static @api SrvOauth get(Long id){		
		return getSrvOauthById(id);
	}
	/**
	 * 设置ID
	 * @param id
	 * @return
	 */
	public void setId(Long id){		
		this.soId = id;
	}
	
	/**
	 * 获取所有用户授权
	 * @return
	 */
	public static @api List<SrvOauth> list(Map params,Integer size){
		return getSrvOauthList(params,size);
	}
	/**
	 * 获取用户授权分页
	 * @return
	 */
	public static @api Page<SrvOauth> page(Integer page,Integer size,Map params){
		return getSrvOauthPage(page, size , params);
	}
	
	
	/**
	 * 根据查询条件取用户授权
	 * @param params
	 * @return
	 */
	public static @api SrvOauth Get(Map params){
		return getSrvOauth(params);
	}
	/**
	 * 获取用户授权数
	 * @return
	 */
	public static @api Long count(Map params){
		return getSrvOauthCount(params);
	}
	/**
	 * 获取用户授权数
	 * @return
	 */
	public static @api <T> T eval(String eval,Map params){
		return getSrvOauthEval(eval,params);
	}
	/**
	 * 获取用户授权图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey){
		return getSrvOauthStats(params,sumKey,groupKey,-1);
	}
	/**
	 * 获取用户授权图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey,int dateType){
		return getSrvOauthStats(params,sumKey,groupKey,dateType);
	}
	
	/**
	 * 根据ID取用户授权
	 * @param id
	 * @return
	 */
	public static @api SrvOauth getSrvOauthById(Long id){		
		return getSrvOauthDao().getSrvOauthById(id);
	}
	/**
	 * 获取id
	 * @return
	 */
	public Long getId(){
		return this.soId;
	}
	
	
	/**
	 * 根据ID取用户授权的标识键值
	 * @param id
	 * @return
	 */
	public static @api String getKeyValue(Object key){
		Long id = Utils.parseLong(key);	
		if(id==null || id.longValue()==0 ){
			return null;
		}
		String value = null;
		value = Cache.getValue(SrvOauth.class, id);
		if(value==null){
			SrvOauth srvOauth = get(id);
			if(srvOauth!=null){
				String strValue = srvOauth.getSoToken$();
				if(!"SoToken".equals("SoToken"))
					strValue+="("+srvOauth.getSoToken$()+")";
				Cache.setValue(SrvOauth.class, id ,strValue);
				value = strValue;
			}else{
				Cache.setValue(SrvOauth.class, id ,"{"+id+"}",30);
				value = "{"+id+"}";
			}
		}
		/************LAZY3Q_ID_KEYVALUE************/
		/************LAZY3Q_ID_KEYVALUE************/
		return value;
	}	
	
	
	/**
	 * 获取当前对象的键值
	 * @return
	 */
	public String getKeyValue(){
		/************LAZY3Q_CODE_KEYVALUE************/
		/************LAZY3Q_CODE_KEYVALUE************/
		String keyValue = this.getSoToken$();
		if(!"SoToken".equals("SoToken"))
			keyValue+="("+this.getSoToken$()+")";
		return keyValue;
	}
	
	
	/**
	 * 根据关键字精确查询用户授权对象
	 * @return
	 */
	public static @api SrvOauth find(String key){
		if(Utils.empty(key))return null;
		Map params = new HashMap();
		if(key.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
			params.put("soId", key.toLowerCase().replaceFirst("id:", ""));					
		else{//按标识查询，精确查询
			String strDefinex = "";
			strDefinex+=" or so_token = '"+key.replaceAll("'", "''")+"'";
			strDefinex="(2=1 "+strDefinex+")";
			params.put("definex",strDefinex);
		}
		/************LAZY3Q_CODE_FIND************/
		/************LAZY3Q_CODE_FIND************/
		return SrvOauth.getSrvOauth(params);
	}
	
	
	/**
	 * 根据关键字查询用户授权
	 * @return
	 */
	public static @api List<Map> query(String key,int size,Map params,Boolean bObject){
		if(Utils.empty((String)params.get("asc")) && Utils.empty((String)params.get("desc")))//如果未传入排序字段
			params.put("desc","so_id");//那么，默认以主键降序，传给Dao层
		String strTerm=key;		
		if(!Utils.empty(strTerm)){
			if(strTerm.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
				params.put("soId", strTerm.toLowerCase().replaceFirst("id:", ""));					
			else//按标识查询，模糊查询请带%
			{
				String strDefinex = "";
				strDefinex+=" or so_token like '%"+strTerm.replaceAll("'", "''")+"%'";
				strDefinex="(2=1 "+strDefinex+")";
				params.put("definex",strDefinex);
			}
		}
		/************LAZY3Q_CODE_QUERY************/
		/************LAZY3Q_CODE_QUERY************/
			
		List<SrvOauth> list = page(0,size,params).getResult();
		
		/**
		* OK!取到数据拼成放入Map中，[{value,text,object:{...}},...]
		* value:数据ID，text:数据标识,object,整个对象
		**/
		List<Map> result = new java.util.ArrayList();
		for(SrvOauth srvOauth:list){
			Map map = new HashMap();
			map.put("key",srvOauth.getSoId().toString());
			map.put("value", Utils.js(srvOauth.getKeyValue()));
			if(bObject==true)
				map.put("object",srvOauth);
			result.add(map);
		}
		/************LAZY3Q_AFTER_QUERY************/
		/************LAZY3Q_AFTER_QUERY************/
		
		return result;
	}
	
	
	
	/**
	 * 获取所有用户授权
	 * @return
	 */
	public static @api List<SrvOauth> getSrvOauthList(Map params,Integer size){
		return getSrvOauthDao().getSrvOauthList(params,0,size);
	}
	
	/**
	 * 获取用户授权分页
	 * @return
	 */
	public static @api Page<SrvOauth> getSrvOauthPage(Integer page,Integer size,Map params){
		if(page==null)page=0;
		if(size==null)size=20;
		return getSrvOauthDao().getSrvOauthPage(page, size , params);
	}
	
	/**
	 * 根据查询条件取用户授权
	 * @param params
	 * @return
	 */
	public static @api SrvOauth getSrvOauth(Map params){
		return getSrvOauthDao().getSrvOauth(params);
	}
	
	/**
	 * 保存一个用户授权
	 * @param srvOauth
	 * @return
	 */
	public static @api SrvOauth saveSrvOauth(SrvOauth srvOauth){
		return getSrvOauthDao().saveSrvOauth(srvOauth);
	}
	
	/**
	 * 根据条件更新用户授权
	 * @param values
	 * @param params
	 */
	public static @api int updateSrvOauthByConfirm(Map values, Map params){
		return getSrvOauthDao().updateSrvOauthByConfirm(values,params);
	}
	
	/**
	 * 修改一个用户授权
	 * @param srvOauth
	 * @return
	 */
	public static @api int updateSrvOauth(SrvOauth srvOauth){
		return getSrvOauthDao().updateSrvOauth(srvOauth);
	}
	
	/**
	 * 更新用户授权非空字段
	 * @param srvOauth
	 */
	public static @api int updateSrvOauth$NotNull(SrvOauth srvOauth){
		return getSrvOauthDao().updateSrvOauth$NotNull(srvOauth);
	}
	
	/**
	 * 获取用户授权数
	 * @return
	 */
	public static @api Long getSrvOauthCount(Map params){
		return getSrvOauthDao().getSrvOauthCount(params);
	}
		
		
	/**
	 * 获取用户授权自定义求和表达式,比如求和:eval="sum(id)"
	 * @return
	 */	
	public static @api <T> T getSrvOauthEval(String eval,Map params){
		return getSrvOauthDao().getSrvOauthEval(eval,params);
	}
	/**
	 * 获取用户授权图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> getSrvOauthStats(Map params,C sumKey,C groupKey,int dateType){
		//TODO:图表统计功能未实现
		
		Map<String,Object> groups=new HashMap();//要分组的字段
		Map<String,Object> sums=new HashMap();//要统计的字段
		
		String[][] dateFormats=new String[][]{
				null,
				new String[]{"%Y","yyyy年","Y"},
				new String[]{"%Y-%m","yyyy年MM月","M"},
				new String[]{"%Y%U","yyyy年第ww周","W"},
				new String[]{"%Y-%m-%d","yyyy年MM月dd日","D"},
				new String[]{"%Y-%m-%d %H","yyyy年MM月dd日 HH点","H"},
				new String[]{"%m","MM月"},
				new String[]{"%d","dd号"},
				new String[]{"%H","HH点"} 
		};
		
		//获取要分组的字段，放入Map中
		if(dateType>0){
			groups.put(groupKey.toString(),"DATE_FORMAT({key},'"+dateFormats[dateType][0]+"')");
		}else{
			groups.put(groupKey.toString(),"{key}");
		}
		if(dateType<6 && dateType>0){ 
			params.put("asc", groupKey.toString()); 
			if(params.get(groupKey.name()+"Start")==null){
				params.put(groupKey.name()+"Start", Utils.date(new Date(), -24, dateFormats[dateType][2]));
			}
		}
		
		
		//获取要统计的字段，放入Map中
		if(sumKey.toString().equals("count")){
			sums.put(sumKey.toString(),"count(*)");
		}else{
			sums.put(sumKey.toString(),"sum({key})");
		}
		
		List<Map> list = getSrvOauthDao().getSrvOauthStats(params,groups,sums);
		
		List<Map> result = new ArrayList();
		for(Map m:list){
			Map map = new HashMap();
			Object value = m.get(sumKey.toString());
			Object name = null;
			Object groupValue = m.get(groupKey.toString());
			if(dateType>0){
				name = Utils.date(Utils.parseDate(groupValue), dateFormats[dateType][1]);
			}else{
				try{
					java.lang.reflect.Method method = SrvOauth.class.getMethod("get"+Utils.ForMat(groupKey.toString())+"$");
					method.setAccessible(true);
					name = method.invoke(SrvOauth.fromMap(m));
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
			map.put("name", name);
			map.put("value", value);
			result.add(map);
		}
		return result;
	}
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static int updateSrvOauth(Map set,Map where){
		if(set.isEmpty())throw new RuntimeException("set为空");
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		return getSrvOauthDao().updateSrvOauthByConfirm(set, where);
	}
	
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void deleteSrvOauth(Map where){
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		getSrvOauthDao().deleteSrvOauthByConfirm(where);
	}
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void delete(Long id){
		if(id==null)throw new RuntimeException("id为空");
		getSrvOauthDao().deleteSrvOauthById(id);
	}
	
	
	/**
	 * 保存用户授权对象
	 * @param params
	 * @return
	 */
	public SrvOauth save(){
		if(this.getSoId()!=null)
			getSrvOauthDao().updateSrvOauth(this);
		else
			return getSrvOauthDao().saveSrvOauth(this);
		return this;
	}
	
	
	/**
	 * 更新用户授权对象的非空字段,注意跟Service里面的update不一样哦
	 * @param params
	 * @return
	 */
	public int update(){
		return getSrvOauthDao().updateSrvOauth$NotNull(this);
	}
	
	
	/**
	 * 删除
	 * @param params
	 * @return
	 */
	public void delete(){
			getSrvOauthDao().deleteSrvOauthById(this.getSoId());
	}
	
	/**
	 * 执行事务
	 * @param function
	 */
	public static <T> T execute(Function function){
		return getSrvOauthDao().executeTransaction(function);
	}
	/*******************************编号**********************************/	
	/**
	* 编号 [非空]      
	**/
	public Long getSoId(){
		if(this.soId!=null){
			return this.soId;
		}
		return this.soId;
	}
	/**
	* 获取编号格式化(toString)
	**/
	public String getSoId$(){
		String strValue="";
		 strValue=Utils.string(this.getSoId());
	 	 return strValue;
	}
	/**
	* 根据编号反格式化
	**/
	public void setSoId$(String strValue){
		if(Utils.empty(strValue)){
			this.soId=null;
			return;
		}
	 	this.soId=Utils.parseLong(strValue);
	}
	
	/**
	* 编号 [非空]      
	**/
	public void setSoId(Long soId){
		this.soId = soId;
		this.setSeted(F.soId);
	}
	
	
	
	
	/*******************************Token**********************************/	
	/**
	* Token [非空]      
	**/
	public String getSoToken(){
		if(this.soToken!=null){
			return this.soToken;
		}
		return this.soToken;
	}
	/**
	* 获取Token格式化(toString)
	**/
	public String getSoToken$(){
		String strValue="";
		 strValue=Utils.string(this.getSoToken());
	 	 return strValue;
	}
	/**
	* 根据Token反格式化
	**/
	public void setSoToken$(String strValue){
		if(Utils.empty(strValue)){
			this.soToken=null;
			return;
		}
	 	this.soToken=strValue;
	}
	
	/**
	* Token [非空]      
	**/
	public void setSoToken(String soToken){
		this.soToken = soToken;
		this.setSeted(F.soToken);
	}
	
	
	
	
	/*******************************帐号ID**********************************/	
	/**
	* 帐号ID [非空]      
	**/
	public Long getSoUserId(){
		if(this.soUserId!=null){
			return this.soUserId;
		}
		return this.soUserId;
	}
	/**
	* 获取帐号ID格式化(toString)
	**/
	public String getSoUserId$(){
		String strValue="";
		 strValue=Utils.string(this.getSoUserId());
	 	 return strValue;
	}
	/**
	* 根据帐号ID反格式化
	**/
	public void setSoUserId$(String strValue){
		if(Utils.empty(strValue)){
			this.soUserId=null;
			return;
		}
	 	this.soUserId=Utils.parseLong(strValue);
	}
	
	/**
	* 帐号ID [非空]      
	**/
	public void setSoUserId(Long soUserId){
		this.soUserId = soUserId;
		this.setSeted(F.soUserId);
	}
	
	
	
	
	/*******************************API客户端**********************************/	
	/**
	* API客户端 [非空] [SrvApiClient]     
	**/
	public Long getSoApiClient(){
		if(this.soApiClient!=null){
			return this.soApiClient;
		}
		return this.soApiClient;
	}
	/**
	* 获取API客户端格式化(toString)
	**/
	public String getSoApiClient$(){
		String strValue="";
		if(this.getSoApiClient()!=null){
				strValue+=Utils.string(SrvApiClient.getKeyValue(this.getSoApiClient()));
		}			
	 	 return strValue;
	}
	/**
	* 根据API客户端反格式化
	**/
	public void setSoApiClient$(String strValue){
		if(Utils.empty(strValue)){
			this.soApiClient=null;
			return;
		}
		SrvApiClient item = SrvApiClient.find(strValue);
		if(item!=null){
			this.soApiClient = Utils.parseLong(item.getSacId());
		}
	}
	
	/**
	* API客户端 [非空] [SrvApiClient]     
	**/
	public void setSoApiClient(Long soApiClient){
		this.soApiClient = soApiClient;
		this.setSeted(F.soApiClient);
	}
	
	
	
	
	/**
	* 获取关联对象[API客户端]
	**/	 			
 	public SrvApiClient get$soApiClient(){
 		if(this.$soApiClient!=null)
 			return this.$soApiClient;
 		com.distribute.framework.model.SrvApiClient srvApiClient = null;
		if(this.getSoApiClient()!=null){
 			srvApiClient = SrvApiClient.get(this.getSoApiClient());
 		}
 		return srvApiClient;
	}
	/*******************************三方标识**********************************/	
	/**
	* 三方标识       
	**/
	public String getSoTicket(){
		if(this.soTicket!=null){
			return this.soTicket;
		}
		return this.soTicket;
	}
	/**
	* 获取三方标识格式化(toString)
	**/
	public String getSoTicket$(){
		String strValue="";
		 strValue=Utils.string(this.getSoTicket());
	 	 return strValue;
	}
	/**
	* 根据三方标识反格式化
	**/
	public void setSoTicket$(String strValue){
		if(Utils.empty(strValue)){
			this.soTicket=null;
			return;
		}
	 	this.soTicket=strValue;
	}
	
	/**
	* 三方标识       
	**/
	public void setSoTicket(String soTicket){
		this.soTicket = soTicket;
		this.setSeted(F.soTicket);
	}
	
	
	
	
	/*******************************三方参数**********************************/	
	/**
	* 三方参数       
	**/
	public String getSoParams(){
		if(this.soParams!=null){
			return this.soParams;
		}
		return this.soParams;
	}
	/**
	* 获取三方参数格式化(toString)
	**/
	public String getSoParams$(){
		String strValue="";
		 strValue=Utils.string(this.getSoParams());
	 	 return strValue;
	}
	/**
	* 根据三方参数反格式化
	**/
	public void setSoParams$(String strValue){
		if(Utils.empty(strValue)){
			this.soParams=null;
			return;
		}
	 	this.soParams=strValue;
	}
	
	/**
	* 三方参数       
	**/
	public void setSoParams(String soParams){
		this.soParams = soParams;
		this.setSeted(F.soParams);
	}
	
	
	
	
	/*******************************登录时间**********************************/	
	/**
	* 登录时间 [非空]      
	**/
	public Date getSoUpdateTime(){
		if(this.soUpdateTime!=null){
			return this.soUpdateTime;
		}
		return this.soUpdateTime;
	}
	/**
	* 获取登录时间格式化(toString)
	**/
	public String getSoUpdateTime$(){
		String strValue="";
		 strValue=Utils.string(Utils.date(this.getSoUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
	 	 return strValue;
	}
	/**
	* 根据登录时间反格式化
	**/
	public void setSoUpdateTime$(String strValue){
		if(Utils.empty(strValue)){
			this.soUpdateTime=null;
			return;
		}
	 	this.soUpdateTime=Utils.date(strValue,"yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	* 登录时间 [非空]      
	**/
	public void setSoUpdateTime(Date soUpdateTime){
		this.soUpdateTime = soUpdateTime;
		this.setSeted(F.soUpdateTime);
	}
	
	
	
	
	/************LAZY3Q_DEFINE_CODE************/
	/************LAZY3Q_DEFINE_CODE************/
	
	
	/**
	 * 触发器
	 */
	public static void on(Trigger.Index index,final Notify.Listener listener,Trigger.Type... types){
		Notify.on(SrvOauth.class, index, listener, types);
	}
	public static void on(final Notify.Listener listener,String type){
		Notify.on(SrvOauth.class, listener, type);
	}
	/**
	 * 当修改字段时
	 * @param filedName
	 * @param listener
	 */
	public static void on(String filedName,final Notify.Listener listener){
		Notify.on(SrvOauth.class, listener, filedName);
	}
	/**
	 * 当修改字段为某值时
	 * @param filedName
	 * @param value
	 * @param listener
	 */
	public static void on(String filedName,Object value,final Notify.Listener listener){
		if(value==null)return;
		Notify.on(SrvOauth.class, listener, filedName+"."+Utils.md5(""+value));
	}
	public void notify(String type){
		Trigger.on(SrvOauth.class,type, this);
	}
	
	
	/**
	 * 用来获取当有字段关联对象时的获取方式,调用如${SrvOauth.obj.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${SrvOauth.$filedName}获取关联对象
	 */
	public Map getObj(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return SrvOauth.class.getMethod("get$"+Utils.forMat(key.toString())).invoke(SrvOauth.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 用来获取某字段格式化的获取方式,调用如${SrvOauth.fmt.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${SrvOauth.filedName$}获取某字段格式化
	 */
	public Map getFmt(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return SrvOauth.class.getMethod("get"+Utils.ForMat(key.toString()+"$")).invoke(SrvOauth.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 获取查询参数链式串
	 * @return
	 */
	public static @Deprecated M where(){ return new M(0); }
	public static @Deprecated M m(){ return new M(0); }
	public static M where = new M(0,true);
	public static @database(DatabaseNames.distribute_sys) class M extends IbatisMap{
		int type = 0; 
		M linked = null;
		Boolean proxy = false;
		public M set = null;
		public M(int type){
			this.type = type;
			if(this.type==0){
				set = new M(1,true);
				set.linked = this;
			}
		};
		public M(int type,Boolean proxy){
			this.type = type;
			this.proxy=proxy;
		};
		public M proxy(){
			M m = new M(this.type);//当前的对像
			if(this.type==0){
				m.set = new M(1,true);//set对象
				m.set.linked = m;
			}else{
				m.linked=this.linked;
				this.linked.linked=m;
			}
			return m;
		}
		public @Deprecated M set(){			
			M m = this;
			m.linked=new M(1);
			m.linked.linked=m;
			return m.linked;
		};
		public M on(IbatisMap value,C column,String joinColumn){if(proxy)return proxy().on(value,column,joinColumn);this.put(value.getClass().getDeclaringClass().getSimpleName(), value);value.put("on", column.toString());value.put("to", joinColumn);return this;};
		/** 编号 [非空]       **/
		public M soId(Object soId){if(proxy)return proxy().soId(soId);  this.put("soId", soId);return this;};
	 	/** and so_id is null */
 		public M soIdNull(){if(proxy)return proxy().soIdNull();if(this.get("soIdNot")==null)this.put("soIdNot", "");this.put("soId", null);return this;};
 		/** not .... */
 		public M soIdNot(){if(proxy)return proxy().soIdNot();this.put("soIdNot", "not");return this;};
 		public M soIdNotNull(){if(proxy)return proxy().soIdNotNull();this.put("soIdNot", "not");this.put("soId", null);return this;};
 		public M soIdNot(Object soId){if(proxy)return proxy().soIdNot(soId);this.put("soIdNot", "not");this.put("soId", soId);return this;};
		/** and so_id <= ? */
		public M soIdMin(Object min){if(proxy)return proxy().soIdMin(min);this.put("soIdMin", min);return this;};
		/** and so_id >= ? */
		public M soIdMax(Object max){if(proxy)return proxy().soIdMax(max);this.put("soIdMax", max);return this;};
		public M soIdIn(String ids){
			if(proxy)return proxy().soIdIn(ids);  this.put("soIdIn", ids);return this;
		};	
		/** Token [非空]       **/
		public M soToken(Object soToken){if(proxy)return proxy().soToken(soToken);  this.put("soToken", soToken);return this;};
	 	/** and so_token is null */
 		public M soTokenNull(){if(proxy)return proxy().soTokenNull();if(this.get("soTokenNot")==null)this.put("soTokenNot", "");this.put("soToken", null);return this;};
 		/** not .... */
 		public M soTokenNot(){if(proxy)return proxy().soTokenNot();this.put("soTokenNot", "not");return this;};
 		public M soTokenNotNull(){if(proxy)return proxy().soTokenNotNull();this.put("soTokenNot", "not");this.put("soToken", null);return this;};
 		public M soTokenNot(Object soToken){if(proxy)return proxy().soTokenNot(soToken);this.put("soTokenNot", "not");this.put("soToken", soToken);return this;};
		/** 帐号ID [非空]       **/
		public M soUserId(Object soUserId){if(proxy)return proxy().soUserId(soUserId);  this.put("soUserId", soUserId);return this;};
	 	/** and so_user_id is null */
 		public M soUserIdNull(){if(proxy)return proxy().soUserIdNull();if(this.get("soUserIdNot")==null)this.put("soUserIdNot", "");this.put("soUserId", null);return this;};
 		/** not .... */
 		public M soUserIdNot(){if(proxy)return proxy().soUserIdNot();this.put("soUserIdNot", "not");return this;};
 		public M soUserIdNotNull(){if(proxy)return proxy().soUserIdNotNull();this.put("soUserIdNot", "not");this.put("soUserId", null);return this;};
 		public M soUserIdNot(Object soUserId){if(proxy)return proxy().soUserIdNot(soUserId);this.put("soUserIdNot", "not");this.put("soUserId", soUserId);return this;};
		/** and so_user_id <= ? */
		public M soUserIdMin(Object min){if(proxy)return proxy().soUserIdMin(min);this.put("soUserIdMin", min);return this;};
		/** and so_user_id >= ? */
		public M soUserIdMax(Object max){if(proxy)return proxy().soUserIdMax(max);this.put("soUserIdMax", max);return this;};
		public M soUserIdIn(String ids){
			if(proxy)return proxy().soUserIdIn(ids);  this.put("soUserIdIn", ids);return this;
		};	
		public M soApiClient$on(SrvApiClient.M value){if(proxy)return proxy().soApiClient$on(value);this.put("SoApiClient", value);value.put("on", "so_api_client");return this;};
		/** API客户端 [非空] [SrvApiClient]      **/
		public M soApiClient(Object soApiClient){if(proxy)return proxy().soApiClient(soApiClient);  this.put("soApiClient", soApiClient);return this;};
	 	/** and so_api_client is null */
 		public M soApiClientNull(){if(proxy)return proxy().soApiClientNull();if(this.get("soApiClientNot")==null)this.put("soApiClientNot", "");this.put("soApiClient", null);return this;};
 		/** not .... */
 		public M soApiClientNot(){if(proxy)return proxy().soApiClientNot();this.put("soApiClientNot", "not");return this;};
 		public M soApiClientNotNull(){if(proxy)return proxy().soApiClientNotNull();this.put("soApiClientNot", "not");this.put("soApiClient", null);return this;};
 		public M soApiClientNot(Object soApiClient){if(proxy)return proxy().soApiClientNot(soApiClient);this.put("soApiClientNot", "not");this.put("soApiClient", soApiClient);return this;};
		public M soApiClientIn(String ids){
			if(proxy)return proxy().soApiClientIn(ids);  this.put("soApiClientIn", ids);return this;
		};	
		/** 三方标识        **/
		public M soTicket(Object soTicket){if(proxy)return proxy().soTicket(soTicket);  this.put("soTicket", soTicket);return this;};
	 	/** and so_ticket is null */
 		public M soTicketNull(){if(proxy)return proxy().soTicketNull();if(this.get("soTicketNot")==null)this.put("soTicketNot", "");this.put("soTicket", null);return this;};
 		/** not .... */
 		public M soTicketNot(){if(proxy)return proxy().soTicketNot();this.put("soTicketNot", "not");return this;};
 		public M soTicketNotNull(){if(proxy)return proxy().soTicketNotNull();this.put("soTicketNot", "not");this.put("soTicket", null);return this;};
 		public M soTicketNot(Object soTicket){if(proxy)return proxy().soTicketNot(soTicket);this.put("soTicketNot", "not");this.put("soTicket", soTicket);return this;};
		/** 三方参数        **/
		public M soParams(Object soParams){if(proxy)return proxy().soParams(soParams);  this.put("soParams", soParams);return this;};
	 	/** and so_params is null */
 		public M soParamsNull(){if(proxy)return proxy().soParamsNull();if(this.get("soParamsNot")==null)this.put("soParamsNot", "");this.put("soParams", null);return this;};
 		/** not .... */
 		public M soParamsNot(){if(proxy)return proxy().soParamsNot();this.put("soParamsNot", "not");return this;};
 		public M soParamsNotNull(){if(proxy)return proxy().soParamsNotNull();this.put("soParamsNot", "not");this.put("soParams", null);return this;};
 		public M soParamsNot(Object soParams){if(proxy)return proxy().soParamsNot(soParams);this.put("soParamsNot", "not");this.put("soParams", soParams);return this;};
		/** 登录时间 [非空]       **/
		public M soUpdateTime(Object soUpdateTime){if(proxy)return proxy().soUpdateTime(soUpdateTime);  this.put("soUpdateTime", soUpdateTime);return this;};
	 	/** and so_update_time is null */
 		public M soUpdateTimeNull(){if(proxy)return proxy().soUpdateTimeNull();if(this.get("soUpdateTimeNot")==null)this.put("soUpdateTimeNot", "");this.put("soUpdateTime", null);return this;};
 		/** not .... */
 		public M soUpdateTimeNot(){if(proxy)return proxy().soUpdateTimeNot();this.put("soUpdateTimeNot", "not");return this;};
 		public M soUpdateTimeNotNull(){if(proxy)return proxy().soUpdateTimeNotNull();this.put("soUpdateTimeNot", "not");this.put("soUpdateTime", null);return this;};
 		public M soUpdateTimeNot(Object soUpdateTime){if(proxy)return proxy().soUpdateTimeNot(soUpdateTime);this.put("soUpdateTimeNot", "not");this.put("soUpdateTime", soUpdateTime);return this;};
 		/** and so_update_time >= ? */
 		public M soUpdateTimeStart(Object start){if(proxy)return proxy().soUpdateTimeStart(start);this.put("soUpdateTimeStart", start);return this;};			
 		/** and so_update_time <= ? */
 		public M soUpdateTimeEnd(Object end){if(proxy)return proxy().soUpdateTimeEnd(end);this.put("soUpdateTimeEnd", end);return this;};
	 	public M add(String key, Object value) {if(proxy)return proxy().add(key,value);this.put(key, value);return this;}
	 	public M add(F key, Object value) {if(proxy)return proxy().add(key,value);this.put(key.toString(), value);return this;}
	 	public M desc(C key) {if(proxy)return proxy().desc(key);this.put("desc", key.column);return this;}
	 	public M asc(C key) {if(proxy)return proxy().asc(key);this.put("asc", key.column);return this;}
	 	public M desc(String sql) {if(proxy)return proxy().desc(sql);this.put("desc", sql);return this;}
	 	public M asc(String sql) {if(proxy)return proxy().asc(sql);this.put("asc", sql);return this;}
	 	public M definex(String sql) {if(proxy)return proxy().definex(sql);this.put("definex", sql);return this;}
	 	/** 获取所有用户授权 **/
		public @api List<SrvOauth> list(Integer size){			
			return getSrvOauthList(this,size);
		}
		/** 获取用户授权分页 **/
		public @api Page<SrvOauth> page(Integer page,Integer size){			
			return getSrvOauthPage(page, size , this);
		}
		/** 根据查询条件取用户授权 **/
		public @api SrvOauth get(){			
			return getSrvOauth(this);
		}
		/** 获取用户授权数 **/
		public @api Long count(){			
			return getSrvOauthCount(this);
		}
		/** 获取用户授权表达式 **/
		public @api <T> T eval(String strEval){			
			return getSrvOauthEval(strEval,this);
		}
		/** 获取用户授权统计 **/
		public @api List<Map> stats(C sumKey,C groupKey,int dateType){			
			return getSrvOauthStats(this,sumKey,groupKey,dateType);
		}
		/** 获取用户授权统计 **/
		public @api List<Map> stats(C sumKey,C groupKey){			
			return getSrvOauthStats(this,sumKey,groupKey,-1);
		}
		/** 根据条件更新 **/
		public @api int update(){			
			M where = null;
			M set = null;
			if(this.type==0){
				if(this.linked==null)throw new RuntimeException("未发set");
				where = this;
				set = this.linked;
			}else{
				if(this.linked==null)throw new RuntimeException("未发where条件");
				set = this;
				where = this.linked;
			}
			return updateSrvOauth(set,where);
		}
		/** 根据条件删除 **/
		public @api void delete(){			
			deleteSrvOauth(this);
		}
	}
	
	public static class Param extends M{
		public Param(int type) {
			super(type);
		}
		public Param(int type, Boolean proxy) {
			super(type, proxy);
		}
	}
	
	/** 对象的字段描述 **/
	public static class F{
		/** 编号 [非空]       **/
		public final static @type(Long.class)  String soId="soId";
		/** Token [非空]       **/
		public final static @type(String.class)  String soToken="soToken";
		/** 帐号ID [非空]       **/
		public final static @type(Long.class)  String soUserId="soUserId";
		/** and so_user_id >= ? */
		public final static @type(Long.class) String soUserIdMin="soUserIdMin";
		/** and so_user_id <= ? */
		public final static @type(Long.class) String soUserIdMax="soUserIdMax";
		/** API客户端 [非空] [SrvApiClient]      **/
		public final static @type(Long.class)  String soApiClient="soApiClient";
		/** 三方标识        **/
		public final static @type(String.class)  String soTicket="soTicket";
		/** 三方参数        **/
		public final static @type(String.class)  String soParams="soParams";
		/** 登录时间 [非空]       **/
		public final static @type(Date.class)  String soUpdateTime="soUpdateTime";
	 	/** and so_update_time >= ? */
 		public final static @type(Date.class) String soUpdateTimeStart="soUpdateTimeStart";
 		/** and so_update_time <= ? */
 		public final static @type(Date.class) String soUpdateTimeEnd="soUpdateTimeEnd";
	 	public static String get(String name){
			try {
				Field field = F.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	/** 对象的数据库字段描述 **/
	public static enum C{
		soId("so_id"),
		soToken("so_token"),
		soUserId("so_user_id"),
		soApiClient("so_api_client"),
		soTicket("so_ticket"),
		soParams("so_params"),
		soUpdateTime("so_update_time"),
		count("count");
		String column;
		C(String column){
			this.column = column;
		}
		public String toString(){
			return this.column;
		}
		public static String get(String name){
			try {
				Field field = C.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	
	
	
	/**
	* 获取用户授权数据
	**/
	public static Object getData(Object param){
		if(param==null)
			return null;
		Object value = null;
		if(param.getClass()==Long.class)
			value = SrvOauth.get((Long)param);
		else if(param instanceof java.util.Map){
			java.util.Map params = (Map) param;
			if(params.get("size")==null)
				value = SrvOauth.getSrvOauth(params);
			else
				value = SrvOauth.getSrvOauthList(params, (Integer) params.get("size"));
		}else if(param.getClass()==Long.class )
			value =  SrvOauth.where().soId(param).get();
		else if(!Utils.empty(param.toString()))
			value = SrvOauth.get(Long.valueOf(param.toString()));
		return value;
	}
	
	/**对象转json**/
	public Map toJSON(){
		return toJSON(false);
	}
	public Map toJSON(boolean simple){
		Map map = new HashMap();
		map.put("id",this.getSoId());
		Utils.fms(map,simple?"id":"soId",this.getSoId(),this.getSoId$());
		Utils.fms(map,simple?"token":"soToken",this.getSoToken(),this.getSoToken$());
		Utils.fms(map,simple?"userId":"soUserId",this.getSoUserId(),this.getSoUserId$());
		Utils.fms(map,simple?"apiClient":"soApiClient",this.getSoApiClient(),this.getSoApiClient$());
		Utils.fms(map,simple?"ticket":"soTicket",this.getSoTicket(),this.getSoTicket$());
		Utils.fms(map,simple?"params":"soParams",this.getSoParams(),this.getSoParams$());
		Utils.fms(map,simple?"updateTime":"soUpdateTime",this.getSoUpdateTime(),this.getSoUpdateTime$());
		if(this.suffix!=null)map.put("suffix", this.suffix);
		/************LAZY3Q_DEFINE_TOJSON************/
		/************LAZY3Q_DEFINE_TOJSON************/
		return map;
	}
	/**对象转map**/
	public Map toMap(){
		Map map = new HashMap();
		map.put("soId",this.getSoId());
		map.put("soToken",this.getSoToken());
		map.put("soUserId",this.getSoUserId());
		map.put("soApiClient",this.getSoApiClient());
		map.put("soTicket",this.getSoTicket());
		map.put("soParams",this.getSoParams());
		map.put("soUpdateTime",this.getSoUpdateTime());
		map.put("suffix", this.suffix);
		/************LAZY3Q_DEFINE_TOMAP************/
		/************LAZY3Q_DEFINE_TOMAP************/
		return map;
	}
	/**map转对象**/
	public static SrvOauth fromMap(Map map){
		SrvOauth srvOauth = new SrvOauth();
		srvOauth.soId = Utils.parseLong(map.get("soId"));
		srvOauth.soToken = Utils.parseString(map.get("soToken"));
		srvOauth.soUserId = Utils.parseLong(map.get("soUserId"));
		srvOauth.soApiClient = Utils.parseLong(map.get("soApiClient"));
		srvOauth.soTicket = Utils.parseString(map.get("soTicket"));
		srvOauth.soParams = Utils.parseString(map.get("soParams"));
		srvOauth.soUpdateTime = Utils.parseDate(map.get("soUpdateTime"));
		return srvOauth;
	}
	
	
	
	/**字段设置标记队列**/
	private Map<String,Boolean> seted=new HashMap();
	public Boolean getSeted(String key){
		return seted.get(key);
	}
	public void setSeted(String key){
		seted.put(key,true);
	}
	/**同步未set的字段**/
	public void mergeSet(SrvOauth old){
		Field[] fields = this.getClass().getDeclaredFields();
		try {
			for(Field field:fields){
				String key = field.getName();
				if(seted.get(key)==null && field.getAnnotation(column.class)!=null){
					field.setAccessible(true);
					field.set(this, field.get(old));					
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
		
	/**自定义变量存储**/
	private Map values=new HashMap();
	public Map getValues(){
		return values;
	}
	public void setValues(String name,Object value){
		values.put(name,value);
	}
	
	/**分表时的表名后缀**/
	private String suffix;
	public String getSuffix() {
		return suffix;
	}
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}
	/**获取数据库名称**/
	public static String getDataBaseName(){
		return Utils.config("spring.datasource.distribute_sys.name");
	}
	
	public static String getIcon(){
		return "/images/icons/Keychain2.png";
	}
	public static String getIconfont(){
		return "icon-suo4";
	}
	public static enum ACT{
		列表,添加,修改,详情
	};
	public static String getUrl(ACT act,SrvOauth.M params){
		if(params==null)params=new SrvOauth.M(0);
		params.remove("joins");
		params.remove("definexs");
		params.remove("distinct");
		String strUrl = "/admin/api/oauth.edit.html?";
		if(params.get("soId")!=null){
			strUrl+="&id="+Utils.encode(""+params.get("soId"),Utils.UTF8);
		}
		if(act==ACT.列表){
			strUrl = "/admin/api/oauth.html?1=1";
		}else if(act==ACT.添加){
			strUrl+="&edittype=insert";
		}else if(act==ACT.修改){
			strUrl+="&edittype=update";
		}else if(act==ACT.详情){
			strUrl+="&edittype=detail";
		}		
		if(params!=null){
			for(Object key:params.keySet()){
				strUrl+="&"+key+"="+Utils.encode(""+params.get(key),Utils.UTF8)+"";
			}
		}
		return strUrl;
	}
	public String getDetailUrl(){
		return this.getUrl(ACT.详情, where.soId(this.getId()));
	}
	
	/**
	* 转跳到当前地址
	**/
	public static Protocol redirect(SrvOauth.ACT act,SrvOauth.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_REDIRECT");
		protocol.set("url",SrvOauth.getUrl(act, params));
		return protocol;
	}
	/**
	* 打开一个新的tab页面
	**/
	public static Protocol href(SrvOauth.ACT act,SrvOauth.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_HREF");
		protocol.set("url",SrvOauth.getUrl(act, params));
		protocol.set("title",act.name());
		return protocol;
	}
	/**
	* 显示一个消息框，并且关闭当前的对话框
	**/
	public static Protocol message(String message){
		Protocol protocol = Protocol.create(true,null,"ACT_MESSAGE");
		protocol.set("message",message);
		return protocol;
	}
	
	
	static com.distribute.framework.dao.ISrvOauthDao getSrvOauthDao(){
		return Utils.getBean("srvOauthDao");
	};
	
}